<?php
declare(strict_types=1);

namespace ServerApp\lib\server\model\mysql;

use WebApp\lib\db\Table;

/**
 * Class push_message_delay
 * @package Tao\src\model
 */
class PushMessageDelay extends Table
{
    const MID = 'mid';
    const TO = 'to';
    const FROM = 'from';
    const CMD = 'cmd';
    const SUB_CMD = 'sub_cmd';
    const DATA = 'data';
    const TIME_EXP = 'time_exp';
    const TIME_CREATE = 'time_create';
    const TIME_BEGIN = 'time_begin';
    const STATUS = 'status';
    const KEY = 'key';
    const DATA_FLAG = 'data_flag';
    const DESC_ID = 'desc_id';

    /**
     * push_message_delay constructor.
     */
    private function __construct()
    {
        $this->init
    }

    /**
     * @var PushMessageDelay
     */
    private static $instance = null;

    /**
     * @return PushMessageDelay
     */
    public static function getInstance(): PushMessageDelay
    {
        if (self::$instance == null) {
            self::$instance = new self();
            self::$instance->init(__NAMESPACE__, __CLASS__, '', '');
        }
        return self::$instance;
    }

    /**
     * @param $mid
     * @param int $limit
     * @return array
     * @internal param int $uid
     * @throws \ErrorException
     */
    public function get_push_message_by_cmd($mid, $limit = 500)
    {
        $now = date('Y-m-d H:i:s');

        $sql = sprintf("select * from %s where
" . self::STATUS . "=0 and " . self::MID . ">$mid and
" . self::TIME_BEGIN . "<'$now' and
" . self::TIME_EXP . ">'$now' order by " . self::MID . " asc limit {$limit}",
            $this->getTableName()
        );
//        echo $sql."\n";
        return $this->getObjArrBySql($sql);
    }
}